@extends('Home.Index.index')
@section('title')
    结算页
@endsection
@section('content')
    <section id="main" class="entire_width">
        <div class="container_12">
            <div class="grid_12">
                <h1 class="page_title">结算中心</h1>
                {{--收货地址列表--}}
                <div class="address_content">
                    <table cellpadding="0" cellspacing="0" class="user_address" width="100%">
                        <thead>
                        <tr class="label">
                            <td width="80px;">收货人姓名</td>
                            <td width="220px;">电话</td>
                            <td width="80px;">邮编</td>
                            <td width="120px;">收货地址</td>
                            <td width="80px;">选择</td>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($address as $v)
                        <tr>
                            <td>{{$v->nickname}}</td>
                            <td>{{$v->mobile}}</td>
                            <td>{{$v->postcode}}</td>
                            <td>{{$v->address}}-{{$v->address_info}}</td>
                            <td><input type="checkbox" name="check" id="{{$v->id}}" value=""></td>
                        </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
                <table class="cart_product">
                    <tr>
                        <th class="images"></th>
                        <th class="name">商品名称</th>
                        <th class="qty">商品数量</th>
                        <th class="close">单价</th>
                        <th class="price">小计</th>
                        <th class="subtotal">删除</th>
                    </tr>
                    {{--这里写购物车列表--}}
                    @foreach($goods_list as $list)
                    <tr>
                        {{--购物车商品图片和商品详情--}}
                        <td class="images"><a href=""><img src="{{$list->img_src}}" alt=""></a></td>
                        @if(empty($list->property))
                        <td class="name">{{$list->goods_name}}</td>
                        @else
                            <td class="name">{{$list->goods_name}}&nbsp;<br>{{$list->property}}</td>
                            @endif
                        <td class="qty"><input type="text" name="num" value="{{$list->goods_count}}"  placeholder="{{$list->goods_count}}"/></td>
                        <td class="close">￥{{$list->price}}</td>
                        <td class="price">￥<span class="num_price">{{$list->price*$list->goods_count}}</span></td>
                        <td class="subtotal"><a title="close" class="close" onclick="class_del(this,'{{$list->goods_id}}')" ></a></td>
                    </tr>
                    @endforeach
                    <tr>
                        <td colspan="7" class="cart_but">
                            <div>共计<span class="totalNum">0</span>件
                            <p>总价￥<span class="totalPrice">0</span></p>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="7" class="cart_but">
                            <button class="continue"><a href="{{url('home_index')}}">继续购物</a></button>
                            <button class="update">立即结算</button>
                        </td>
                    </tr>
                </table>
            </div><!-- .grid_12 -->
            <div class="clear"></div>
        </div><!-- .container_12 -->
    </section><!-- #main -->
    <div class="clear"></div>
    <script>
        var address_id = '';
        $(function () {
            $(':checkbox[name="check"]').each(function () {
                $(this).click(function () {
                   if($(this).attr('checked'))
                   {
                       $(':checkbox[name="check"]').removeAttr('checked');
                       $(this).attr('checked','checked');
                       address_id = this.id;
                   }
                });
            });

        });
    </script>
    <script>
        function class_del(obj,goods_id) {
            layer.confirm('确认要删除该商品吗？', function (index) {
                $.ajax({
                    type: 'POST',
                    url: '/goods_delete/'+goods_id,
                    dataType: 'json',
                    success: function (e) {
                        if (e.status) {
                            $(obj).parents("tr").remove();
                            layer.msg('删除成功!', {icon: 1, time: 1000});
                            window.location.reload();
                        }else {
                            layer.msg(e.info);
                        }
                    }
                });
            });
        }
    </script>
    <script>
        var totalNum = 0;
        $("input[name='num']").each(function () {
            totalNum += parseInt($(this).val());
        });
        $('.totalNum').text(totalNum);

        var totalPrice = 0;
       $('.num_price').each(function () {
          totalPrice += parseFloat($(this).text());
       });
        $('.totalPrice').text(totalPrice);
    </script>

    <script>
        $(function () {
            $('.update').click(function () {
                if(address_id.length == 0)
                {
                    layer.msg('请选择正确的收货地址',{icon:5});
                }else{
                    $.post('{{url('/user_order/')}}',{id:address_id,order_total:totalPrice},function (e) {
                        if(e.status)
                        {
                            layer.msg(e.info,{icon:6},function () {
                                window.location.reload();
                            });
                        }else {
                            layer.msg(e.info,{icon:5});
                        }
                    });
                }
            });
        });
    </script>
@endsection